﻿using Ewell.BizComponent.Service.IServer;
using Ewell.BizComponent.Service.Model;
using Ewell.BizComponent.Service.Model.Request;
using Ewell.BizComponent.Service.Model.Response;
using Ewell.Interface.Common.Model.Common;
using Ewell.Interface.Common.Model.GateWay;
using Ewell.Interface.Common.Utils;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Diagnostics;
using System.Threading.Tasks;

namespace Ewell.BizComponent.Service.Server
{
    /// <summary>
    /// 读卡
    /// </summary>
    public class ReadCardServer : IReadCardServer
    {
        const string interproxyProName = "bin\\plugin\\Ewell.Interface.Proxy.exe";
        ReadCardResponse IReadCardServer.readCard(ReadCardRequest request)
        {
            request.uuheader = JsonConvert.SerializeObject(LoginInfo.loginUser);
            request.menuId=LoginInfo.menuId;
            InterfaceClientResult clientResult = default;
            try
            {
                clientResult = ProcessHelper.ProcessInvoke(interproxyProName, request);
            }
            catch (Exception ex)
            {
                throw new SpeedException(ex.Message);
            }
            if (clientResult != null && !clientResult.success)
            {
                throw new SpeedException(clientResult?.message);
            }
            if (clientResult.@object?.GetType() == typeof(JObject))
                return ((JObject)clientResult.@object).ToObject<ReadCardResponse>();
            else
                throw new SpeedException("读卡失败!");
        }
    }
}
